<?php
Loader::using("models::entities::enums::EstadoReporte");
Loader::using("models::entities::Empresas");
Loader::using("models::entities::enums::TipoReporte");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/**
 * Description of ProcesarAmonestacion
 *
 * @author MFerreira
 * @export Modules
 * 
 * export-metadata RequiredRoles:ROLE_PRACTICES_COORDINATOR
 */
class ProcesarAmonestacion extends Module {

    public function index() {
        
      
        $reportes= array();
        $reportes = $this->model->reports->GetByEstado(EstadoReporte::Enviado);
        
     
        
        if($reportes != null){
            
            foreach ($reportes as $reporte) {
                if($reporte->getIdPracticante()!=null){
                    if ($reporte->getPracticante()==null){
                        $reporte->setPracticante($this->model->practitioners->GetById($reporte->getIdPracticante()));
                    }
                }  
            }
            
            
            foreach ($reportes as $reporte) {
                if($reporte->getPracticante()!=null){
                    if ($reporte->getPracticante()->getSolicitud()==null){
                        $reporte->getPracticante()->setSolicitud($this->model->applications->GetById($reporte->getPracticante()->getIdSolicitud()));
                    }
                }  
            }
         
            $this->assign("reportes", $reportes);
            $this->assign("procesar", $this->route->action("Procesar"));
            $this->assign("respondidos", $this->route->action("ReportesRespondidos"));
            
            $this->display();   
            
        }else{
     
            $this->ReportesRespondidos();
           
        }
        
      
    }
    
    public function ReportesRespondidos() {
        $reportes= array();
        $reportes = $this->model->reports->GetByEstado(EstadoReporte::Revisado);
        
     
        
        if($reportes != null){
            foreach ($reportes as $reporte) {
                if($reporte->getIdPracticante()!=null){
                    if ($reporte->getPracticante()==null){
                        $reporte->setPracticante($this->model->practitioners->GetById($reporte->getIdPracticante()));
                    }
                }  
            }
            
            
            foreach ($reportes as $reporte) {
                if($reporte->getPracticante()!=null){
                    if ($reporte->getPracticante()->getSolicitud()==null){
                        $reporte->getPracticante()->setSolicitud($this->model->applications->GetById($reporte->getPracticante()->getIdSolicitud()));
                    }
                }  
            }
          
            $this->assign("reportes", $reportes);
            $this->assign("volver", $this->route->action("index"));
            $this->assign("vermas", $this->route->action("VerMas"));
            $this->display("ReportesRespondidos");   
            
        }else{
            
                  $this->assign("volver", $this->route->action("index"));
                  $this->display("Volver");   
           
        }
        
    }

    public function Procesar() {
        
        Loader::using("models::entities::Reportes");
        
        $idReporte = $this->request->get("idReporte");
        
        /* @var $reporte Reportes */
        
        $reporte = $this->model->reports->GetById($idReporte);
        $practicante = $this->model->practitioners->getById($reporte->getIdPracticante());
        $Solicitud = $this->model->applications->GetById($practicante->getIdSolicitud());
        
        $practicante->setSolicitud($Solicitud);
        $reporte->setPracticante($practicante);
        
        
        $this->assign("Reporte", $reporte);
        $this->assign("action", $this->route->action("Responder"));
        $this->assign("Practicante", $practicante);
        
        $this->display();
    }
    
    public function Responder() {
       Loader::using("models::entities::Reportes");
     
         if($this->request->submitted()){
             
             $reporte = new Reportes();
             
             $reporte = $this->model->reports->GetByID($this->request->post("idReporte"));
             
             $reporte->setDescripcionCoordinador($this->request->post("DescripcionCoordinador"));
             $reporte->setEstadoReporte(EstadoReporte::Revisado);
             
             /* if($reporte->getIdReporte()==TipoReporte::Reemplazo){
                 $solicitud = $this->model->applications->GetById($reporte->getPracticante()->getIdSolicitud());
                 $solicitud->setIdPracticanteActual(0);
                 $this->model->applications->Update($solicitud);
             }*/
             
             $this->model->reports->update($reporte);
             
             $this->addMessage(new Message("", "Reporte Respondido Correctamente !!!", Message::SUCCESS));
             
             $this->assign("volver", $this->route->action("index"));
             
             $this->display();
             
         }else{
             $this->index();
         }
    }
    
       
    public function VerMas() {
       Loader::using("models::entities::Reportes");
             
       /* @var $reporte Reportes */
         $reporte = $this->model->reports->GetByID($this->request->get("idReporte"));
        
         $practicante = $this->model->practitioners->getById($reporte->getIdPracticante());
         $Solicitud = $this->model->applications->GetById($practicante->getIdSolicitud());

         $practicante->setSolicitud($Solicitud);
         $reporte->setPracticante($practicante);
         $this->assign("VolverIndex", $this->route->action("ReportesRespondidos"));
         $this->assign("Reporte", $reporte);
         $this->assign("Practicante", $practicante);
         

         $this->display();
         
    }

    public function getReporte() {
        if (!$this->Reporte) {
            $this->Reporte = new Reportes();
        }
        return $this->Reporte;
    }

}

?>
